<template>
  <div class="dashboard-editor-container">
    <div>
      <el-card>
        <el-form :model="ruleForm" style="margin-top:10px;" size="mini" label-position="top" ref="ruleForm">
          <el-row :gutter="10">
            <el-col :lg="4" :md="6">
              <el-form-item label="批次号" prop='BATCHNO'>
                <el-input size="mini" placeholder="请输入批次号" v-model="ruleForm.BATCHNO"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="开始日期" prop="BATCHBEGINDATA">
                <el-date-picker size="mini" style="width:100%;" v-model="ruleForm.BATCHBEGINDATA" type="date"
                  value-format="yyyyMMdd" :clearable='false' placeholder="开始日期" :picker-options="pickerOptionsStart"
                  @change="changeStart">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="结束日期" prop="BATCHENDDATA">
                <el-date-picker style="width:100%;" size="mini" v-model="ruleForm.BATCHENDDATA" type="date"
                  value-format="yyyyMMdd" placeholder="结束日期" :picker-options="pickerOptionsEnd" @change="changeEnd"
                  :clearable='false'>
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="采集方式" prop="COLLECTFLAG">
                <el-select v-model="ruleForm.COLLECTFLAG" size="mini" clearable placeholder="请选择采集方式">
                  <el-option label="人工" value="1"></el-option>
                  <el-option label="自动" value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :md="12" class="searchBtnMarginTop">
              <el-button type="primary" icon="el-icon-search" :loading="loading" size="mini" @click="queryBatch">查询
              </el-button>
              <el-button type='warning' size='mini' icon='el-icon-refresh' @click="clearForm('ruleForm')">重置</el-button>
              <el-button type="primary" size="mini" icon="el-icon-upload2" @click="openInsert">导入</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
    </div>
    <el-row style="margin-top:20px">
      <el-col>
        <el-card>
          <!-- <div id="download">

          </div> -->
          <el-table :data="tableData" border>
            <el-table-column prop="BATCHNO" label="批次号" align="center" width="110"></el-table-column>
            <el-table-column prop="BATCHDATE" label="批次日期" align="center" width="100"></el-table-column>
            <el-table-column prop="BATCHSTATUS" label="批次状态" align="center" :formatter="dataFormat"></el-table-column>
            <el-table-column prop="COLLECTFLAG" label="采集方式" align="center" :formatter="dataFormat"></el-table-column>
            <el-table-column prop="CHANNELTYPE" label="渠道" width="120" :formatter="dataFormat" align="center">
            </el-table-column>
            <el-table-column prop="SUCCESSNUM" label="成功笔数" align="center"></el-table-column>
            <el-table-column prop="FAILNUM" label="失败笔数" align="center"></el-table-column>
            <el-table-column prop="REFUSENUM" label="过滤笔数" align="center"></el-table-column>
            <el-table-column prop="FILENAME" label="文件名" width="240" align="center"></el-table-column>
            <el-table-column prop="FILEDATE" label="文件日期" width="100" align="center"></el-table-column>
            <el-table-column prop="FILESERNO" label="文件流水" width="100" align="center"></el-table-column>
            <el-table-column prop="COLLECTER" label="采集人" align="center"></el-table-column>
            <el-table-column prop="REMARK1" label="备注"></el-table-column>
            <el-table-column label="导入详情" width="120" align="center" fixed="right">
              <template slot-scope="scope">
                <div>
                  <a target='_blank' :href="fileUrl+scope.row.FAILFILE" size="mini"
                    class="el-button el-button--primary el-button--mini" download="">
                    <i class="el-icon-search"></i>&nbsp;
                    查看</a>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="导入数据" align="center" width="120" fixed="right">
              <template slot-scope="scope">
                <div>
                  <el-button size="mini" type="success" icon="el-icon-document-checked"
                    @click="openSuccessData(scope.row)">查看数据</el-button>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="重新触发" align="center" width="120" fixed="right">
              <template slot-scope="scope">
                <div>
                  <el-button size="mini" v-if="scope.row.BATCHSTATUS=='0'" type="warning" icon="el-icon-setting"
                    @click="openAgain(scope.row)">重新触发</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>

          <div style="text-align:right;margin-top:20px;" v-show="tableData">
            <el-pagination :current-page="currentPage" background @size-change="handleSizeChange"
              :page-sizes="[10,25,50,100]" :page-size="pageSize" layout="total,sizes, prev, pager, next, jumper"
              :total="tableTotal" @current-change="changePage"></el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <keep-alive>
      <component :is="dialogList.excelToJson" :option="dialogExcelOptions" @refreshTable1="queryBatch"></component>
    </keep-alive>
  </div>
</template>

<script>
  // 发送接口
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import request from "@/common/utils/requestQC";
  import dataCollectionQuey from "@/message/QCManage/dataCollectionManage/dataCollection/dataCollectionQuey";
  import toolBox from "@/common/utils/toolBox";
  import dictionary from '@/message/wfm/baseData/dictionaryManage/dictionaryQuery'
  import requestWFM from "@/common/utils/requestWFM";
  //批量上传
  import excelToJson from "./dialog/excelToJson";
  import fileAdd from "@/message/QCManage/dataCollectionManage/dataImport/dataImport";

  export default {
    name: '',
    data() {
      return {
        'collectOption': {
          '1': '人工',
          '0': '自动'
        },
        channelOption: [], //渠道类型
        dialogExcelOptions: {
          isShow: false
        },
        dialogList: {
          excelToJson
        },
        fileUrl: '',
        filePath: getGlobalParams.get("FilePath"),
        pickerOptionsStart: {
          disabledDate: (time) => {
            return time.getTime() > Date.now()
          }
        },
        pickerOptionsEnd: {
          disabledDate: (time) => {
            return time.getTime() > Date.now()
          }
        },
        tableTotal: 0,
        pageSize: undefined,
        currentPage: 1, //表格当前页
        tableData: [],
        ruleForm: {
          BATCHNO: '', //批次号
          BATCHBEGINDATA: '',
          BATCHENDDATA: '',
          COLLECTFLAG: '',
        },
        loading: false,
      }
    },
    computed: {},
    created() {
      this.pageSize = getGlobalParams.get("PageSize");
      this.fileUrl = getGlobalParams.get("FileDownloadAddress") + '/' + getGlobalParams.get("FilePath")
    },
    mounted() {
      this.queryBatch()
      this.dictionaryQuery();
    },
    watch: {},
    methods: {
      // 查看导入成功的数据
      openSuccessData(row) {
        this.$router.push({
          name: '29102', //有问题！自己配置的改动了就会报错
          params: {
            row: row
          }
        });
      },
      // 重新触发
      async openAgain(row) {
        let date = new Date()
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      month = month < 10 ? '0' + month : month
      day = day < 10 ? '0' + day : day
       let nowDate = year + '' + month + '' + day;
        let resBody = new fileAdd();
        resBody.data.COLLECTFLAG = '1' //1-手动导入
        resBody.data.FILENAME = row.FILENAME;
        resBody.data.BATCHDATE = nowDate
        resBody.data.CHANNELTYPE = row.CHANNELTYPE
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.$message({
            type: "success",
            message: "导入成功"
          });
          this.queryBatch()
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          });
        }
      },
      // 查询渠道类型
      async dictionaryQuery() {
        let resBody = new dictionary();
        resBody.data.FIELDCODE = 'CHANNELTYPE'
        resBody.data.CurrPage = '1'
        resBody.data.PageRowNum = '100'
        let response = await requestWFM(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.channelOption = response.BODY.FIEDATA;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      findName(arr, val) {
        let data = arr.filter(item => {
          return item.FIELDKEY == val
        })
        if (data.length)
          return data[0].FIELDKEY + ' - ' + data[0].FIELDVALUE
      },
      dataFormat(row, column) {
        //枚举设备形式
        if (column.property == "CHANNELTYPE")
          return this.findName(this.channelOption, row[column.property]);
        if (column.property == "COLLECTFLAG")
          return this.collectOption[row[column.property]];
        if (column.property == "BATCHSTATUS")
          //0-导入失败 1-导入成功 2-作废 3-待处理
          return row[column.property] == '0' ? '导入失败' : row[column.property] == '1' ? '导入成功' : row[column.property] ==
            '2' ? '作废' : row[column.property] == '3' ? '待处理' : row[column.property];

      },
      // 查询数据采集
      async queryBatch() {
        let resBody = new dataCollectionQuey()
        resBody.data.BATCHNO = this.ruleForm.BATCHNO
        resBody.data.BATCHBEGINDATA = this.ruleForm.BATCHBEGINDATA
        resBody.data.BATCHENDDATA = this.ruleForm.BATCHENDDATA
        resBody.data.COLLECTFLAG = this.ruleForm.COLLECTFLAG
        resBody.data.CurrPage = this.currentPage
        resBody.data.PageRowNum = this.pageSize
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.tableData = response.BODY.struct
          this.tableTotal = response.BODY.TotalRowNum
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      // 打开上传的Dialog
      openInsert() {
        this.dialogExcelOptions.isShow = true;
        this.dialogExcelOptions.channelOption = this.channelOption
      },
      changePage(val) {
        this.currentPage = val;
        this.queryBatch()
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.queryBatch()
      },
      // 获取当天前一天
      getDay(day) {
        var dd = new Date();
        dd.setDate(dd.getDate() + day);
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
        var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
        return y + "" + m + "" + d;
      },
      //  开始日期
      changeStart() {
        this.pickerOptionsEnd = Object.assign({}, this.pickerOptionsEnd, {
          disabledDate: (time) => {
            if (this.ruleForm.BATCHBEGINDATA) {
              return time.getTime() < Date.parse(this.ruleForm.BATCHBEGINDATA.slice(0, 4) + '-' + this.ruleForm
                .BATCHBEGINDATA.slice(4, 6) +
                '-' + this.ruleForm.BATCHBEGINDATA.slice(6, 8)) - 8.64e7 || time.getTime() > Date.now();
            } else {
              return time.getTime() > Date.now();
            }
          }
        })
      },
      // 结束日期
      changeEnd() {
        this.pickerOptionsStart = Object.assign({}, this.pickerOptionsStart, {
          disabledDate: (time) => {
            if (this.ruleForm.BATCHENDDATA) {
              return time.getTime() > Date.parse(this.ruleForm.BATCHENDDATA.slice(0, 4) + '-' + this.ruleForm
                .BATCHENDDATA
                .slice(4, 6) + '-' +
                this.ruleForm.BATCHENDDATA.slice(6, 8)) || time.getTime() > Date.now();
            } else {
              return time.getTime() > Date.now();
            }
          }
        })
      },
      // 重置查询条件
      clearForm(item) {
        // 重置表格
        this.$refs["" + item].resetFields();
      },
    }
  }

</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
